package com.bitinstitute.parkapp.home;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.bitinstitute.parkapp.R;
import com.bitinstitute.parkapp.helper.AlertDialogManager;
import com.bitinstitute.parkapp.helper.InternetConnectionDetector;
import com.bitinstitute.parkapp.helper.TabGroupActivity;
import com.bitinstitute.parkapp.helper.XMLParser;

public class HomeMenuActivity extends Activity {

	// Connection detector
	InternetConnectionDetector cd;

	// Alert dialog manager
	AlertDialogManager alert = new AlertDialogManager();

	// All static variables
	static final String URL = "http://watermark.wa.funpic.de/parkGuide/news/news.xml";

	// XML node keys
	public static final String KEY_ITEM = "item"; // parent node
	public static final String KEY_ID = "id";
	public static final String KEY_NAME = "name";
	public static final String KEY_DATE = "date";
	public static final String KEY_HEADING1 = "heading1";
	public static final String KEY_DESC1 = "description1";
	public static final String KEY_HIGHLIGHT = "highlight";
	public static final String KEY_THUMB_URL = "thumb_url";
	public static final String KEY_WEB_URL = "web_url";

	public Map<String, ?> createItem(String name, String date, String heading1,
			String desc1, String highlight, String thumb_url, String web_url) {
		Map<String, String> item = new HashMap<String, String>();
		item.put(KEY_NAME, name);
		item.put(KEY_DATE, date);
		item.put(KEY_HEADING1, heading1);
		item.put(KEY_DESC1, desc1);
		item.put(KEY_HIGHLIGHT, highlight);
		item.put(KEY_THUMB_URL, thumb_url);
		item.put(KEY_WEB_URL, web_url);
		return item;
	}

	HomeAdapter adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		setContentView(R.layout.home_main);

		// first: check for internet-connectivity
		cd = new InternetConnectionDetector(getApplicationContext());

		// Check if Internet present
		if (!cd.isConnectingToInternet()) {
			// Internet Connection is not present
			alert.showAlertDialog(getParent(), "Internet Connection Error",
					"Please connect to working Internet connection", false);
			// stop executing code by return
			return;
		}

		ArrayList<HashMap<String, String>> newsList = new ArrayList<HashMap<String, String>>();

		XMLParser parser = new XMLParser();
		String xml = parser.getXmlFromUrl(URL); // getting XML from URL
		Document doc = parser.getDomElement(xml); // getting DOM element

		NodeList nl = doc.getElementsByTagName(KEY_ITEM);
		// looping through all animal nodes <item>
		for (int i = 0; i < nl.getLength(); i++) {
			// creating new HashMap
			HashMap<String, String> map = new HashMap<String, String>();
			Element e = (Element) nl.item(i);

			// adding each child node to HashMap key => value

			map.put(KEY_ID, parser.getValue(e, KEY_ID));
			map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
			map.put(KEY_DATE, parser.getValue(e, KEY_DATE));
			map.put(KEY_HIGHLIGHT, parser.getValue(e, KEY_HIGHLIGHT));
			map.put(KEY_HEADING1, parser.getValue(e, KEY_HEADING1));
			map.put(KEY_DESC1, parser.getValue(e, KEY_DESC1));
			map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
			map.put(KEY_WEB_URL, parser.getValue(e, KEY_WEB_URL));

			// adding HashList to ArrayList
			newsList.add(map);
		}

		// Getting adapter by passing xml data ArrayList
		adapter = new HomeAdapter(this, newsList, this);

		adapter.addSection("Park News", new SimpleAdapter(this, newsList,
				R.layout.home_list_row, new String[] { KEY_NAME, KEY_DATE,
						KEY_HIGHLIGHT, KEY_HEADING1, KEY_DESC1, KEY_THUMB_URL,
						KEY_WEB_URL }, new int[] { R.id.name, R.id.date,
						R.id.highlight, R.id.heading1, R.id.desc1,
						R.id.thumb_url, R.id.web_url }));

		final String[] test = new String[] { "Öffnungszeiten", "Preise",
				"Anfahrt" };

		adapter.addSection("Besucherservice", new ArrayAdapter<String>(this,
				R.layout.custom_list_item, android.R.id.text1, test));

		final ListView list = (ListView) findViewById(R.id.listView1);
		list.setAdapter(adapter);

		// Click event for single list row
		list.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				switch (position) {
				case 5:
					Intent openings = new Intent(getParent(), Openings.class);
					TabGroupActivity parentOpenings = (TabGroupActivity) getParent();
					parentOpenings.startChildActivity("Openings", openings);

					break;
				case 6:
					Intent price = new Intent(getParent(), Prices.class);
					TabGroupActivity parentPrice = (TabGroupActivity) getParent();
					parentPrice.startChildActivity("Price", price);

					break;
				case 7:
					Intent approach = new Intent(getParent(), Approach.class);
					TabGroupActivity parentApproach = (TabGroupActivity) getParent();
					parentApproach.startChildActivity("Approach", approach);
					
					break;

				default:

					// getting values from selected ListItem
					String name = ((TextView) view.findViewById(R.id.name))
							.getText().toString();
					String date = ((TextView) view.findViewById(R.id.date))
							.getText().toString();

					String heading1 = ((TextView) view
							.findViewById(R.id.heading1)).getText().toString();
					String desc1 = ((TextView) view.findViewById(R.id.desc1))
							.getText().toString();

					String highlight = ((TextView) view
							.findViewById(R.id.highlight)).getText().toString();
					String thumb_url = ((TextView) view
							.findViewById(R.id.thumb_url)).getText().toString();
					String web_url = ((TextView) view
							.findViewById(R.id.web_url)).getText().toString();

					// Starting new intent
					Intent in = new Intent(getApplicationContext(),
							HomeSingleMenuItemActivity.class);
					in.putExtra(KEY_NAME, name);
					in.putExtra(KEY_DATE, date);
					in.putExtra(KEY_HEADING1, heading1);
					in.putExtra(KEY_DESC1, desc1);
					in.putExtra(KEY_HIGHLIGHT, highlight);
					in.putExtra(KEY_THUMB_URL, thumb_url);
					in.putExtra(KEY_WEB_URL, web_url);

					TabGroupActivity parentActivity = (TabGroupActivity) getParent();
					parentActivity.startChildActivity("SingleMenu", in);

				}

			}
		});

	}
}